
#ifndef _TTG_EXTRAS_XMLTABLE_
#define _TTG_EXTRAS_XMLTABLE_
#include "Common/Definitions.h"

#include <vector>

#include "Common/Extras/Xml/XMLTree.h"
#include "Common/Extras/Xml/TTGXMLTree.h"
#include "Common/Structures.h"

namespace ttg_internal
{
	//Internal class. Contains information about one subsections.
	class XmlTableNode :public ttg::structures::NamedObject
	{
		private:
			ttg::extras::XMLTree *xml;
		public:
			//Creates named node with XMLTree.
			inline XmlTableNode(const char *name, ttg::extras::XMLTree *xml)
				:NamedObject(name)
					{ this->xml = xml; }
			//Returns stored xml.
			inline ttg::extras::XMLTree *getXml()
					{ return xml; }
	};
}

namespace ttg
{
	namespace extras
	{
		//Contains subsections of xml-file and provides Dictionary<XMLTree>-like access.
		class XmlTable
		{
			private:
				TTGXMLTree xml;
				structures::Dictionary<ttg_internal::XmlTableNode> *base;
				std::vector<XMLTree *> refs;
			public:
				//Creates empty table.
				XmlTable();
				//Assigns current table to given xml.
				//"filename": filename of the XML.
				//"path": path to new root section (in xml). E.g. "Data/XSettings".
				//"comparername": name of the attribute, by which value subsections should be compared.
				//In case of any error throws Error().
				void assign(const char *filename, const char *path, const char *comparername);
				//Returns section, which corresponds to "name".
				//If section doesn't exist, returns NULL.
				//Note: returned XMLTree will be released by destructor. But it can released manually too.
				XMLTree *get(const char *name);
				//Returns count of subsections.
				inline size_t getCount() const
						{ return refs.size(); }
				//Returns n-th subsection.
				inline const XMLTree &operator [](const int n) const
						{ return *refs[n]; }
				//Returns n-th subsection.
				inline XMLTree &operator [](const int n)
						{ return refs[n]->getSubXMLTree(); }
				//Clears table and releases all resources.
				void close();
				//Calls "close()".
				~XmlTable();
		};
	}
}

#endif
